#!groovy

node {
  properties([
    parameters([
      [$class: 'ValidatingStringParameterDefinition', 
        defaultValue: '', 
        description: 'The tag of the current release.', 
        failedValidationMessage: 'Invalid SemVer.', 
        name: 'RELEASE_TAG', 
        regex: /^\d+\.\d+\.\d+$/]
    ])
  ])

  try {
    env.AWS_DEFAULT_REGION = "us-east-1"

    // Checkout the proper revision into the workspace.
    stage('checkout') {
      checkout([
        $class: 'GitSCM',
        branches: [[name: params.RELEASE_TAG]],
        extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
        userRemoteConfigs: scm.userRemoteConfigs
      ])
    }

    env.RF_SETTINGS_BUCKET = 'rasterfoundry-testing-config-us-east-1'

    stage('bootstrap') {
      wrap([$class: 'AnsiColorBuildWrapper']) {
        sh """#!/bin/bash
          ./scripts/bootstrap --env
          ./scripts/bootstrap --sbtopts
        """
      }
    }

    stage('publish releases to maven central') {
      withCredentials([[$class: 'StringBinding',
                        credentialsId: 'SONATYPE_USERNAME',
                        variable: 'SONATYPE_USERNAME'],
                        [$class: 'StringBinding',
                        credentialsId: 'SONATYPE_PASSWORD',
                        variable: 'SONATYPE_PASSWORD'],
                        [$class: 'StringBinding',
                        credentialsId: 'GPG_KEY',
                        variable: 'GPG_KEY'],
                        [$class: 'StringBinding',
                        credentialsId: 'GPG_KEY_ID',
                        variable: 'GPG_KEY_ID']]) {
        wrap([$class: 'AnsiColorBuildWrapper']) {
          sh """#!/bin/bash
            docker-compose \
                -f docker-compose.test.yml \
                run --rm --no-deps --entrypoint "bash -c" \
                -e SONATYPE_USERNAME="${SONATYPE_USERNAME}" \
                -e SONATYPE_PASSWORD="${SONATYPE_PASSWORD}" \
                -e GPG_KEY_ID="${GPG_KEY_ID}" \
                build "
                    gpg --keyserver keyserver.ubuntu.com \
                        --recv-keys ${GPG_KEY_ID} &&
                    echo ${GPG_KEY} | base64 -d >signing_key.asc &&
                    gpg --import signing_key.asc &&
                    ./sbt ';set version in ThisBuild := \\"${params.RELEASE_TAG}\\";sonatypeBundleClean;publish;sonatypeBundleRelease'
                "
          """
        }
      }
    }

    slackMessage = "*<https://github.com/raster-foundry/raster-foundry/tree/${params.RELEASE_TAG}|${params.RELEASE_TAG}>* deployed to Maven Central :tada:"
    slackSend color: 'warning', message: slackMessage
  } catch(err) {
    def slackMessage = ":jenkins-angry: *failed to deploy <https://github.com/raster-foundry/raster-foundry/tree/${params.RELEASE_TAG}|${params.RELEASE_TAG}> to Maven Central #${env.BUILD_NUMBER}*"
    slackMessage += "\n<${env.BUILD_URL}|View Build>"
    slackSend color: 'danger', message: slackMessage

    throw err
  }
}
